import Vue from 'vue'
import VueRouter from 'vue-router'
import Main from '../views/Main.vue'
import CategoryEdit from './../views/CategoryEdit'
import CategoryList from './../views/CategoryList'
import ItemsEdit from './../views/ItemsEdit'
import ItemsList from './../views/ItemsList'
import HeroesList from './../views/HeroesList'
import HeroesEdit from './../views/HeroesEdit'
import ArticlesEdit from './../views/ArticlesEdit'
import ArticlesList from './../views/ArticlesList'
import AdsEdit from './../views/AdsEdit'
import AdsList from './../views/AdsList'
import AdminUserEdit from './../views/AdminUserEdit'
import AdminUserList from './../views/AdminUserList'
import Login from './../views/Login'

Vue.use(VueRouter)

const routes = [
  { path: '/login', component: Login, name: Login, meta: { isPublic: true } },
  {
    path: '/',
    name: 'Main',
    component: Main,
    children: [
      { path: '/categories/create', component: CategoryEdit },
      { path: '/categories/list', component: CategoryList },
      // 这个表示把id注入到这个组件中，在这个组件中可以使用props来接收
      { path: '/categories/edit/:id', component: CategoryEdit, props: true },

      { path: '/items/create', component: ItemsEdit },
      { path: '/items/list', component: ItemsList },
      // 这个表示把id注入到这个组件中，在这个组件中可以使用props来接收
      { path: '/items/edit/:id', component: ItemsEdit, props: true },

      { path: '/heroes/create', component: HeroesEdit },
      { path: '/heroes/list', component: HeroesList },
      // 这个表示把id注入到这个组件中，在这个组件中可以使用props来接收
      { path: '/heroes/edit/:id', component: HeroesEdit, props: true },

      { path: '/articles/create', component: ArticlesEdit },
      { path: '/articles/list', component: ArticlesList },
      // 这个表示把id注入到这个组件中，在这个组件中可以使用props来接收
      { path: '/articles/edit/:id', component: ArticlesEdit, props: true },

      { path: '/ads/create', component: AdsEdit },
      { path: '/ads/list', component: AdsList },
      // 这个表示把id注入到这个组件中，在这个组件中可以使用props来接收
      { path: '/ads/edit/:id', component: AdsEdit, props: true },

      { path: '/admin_users/create', component: AdminUserEdit },
      { path: '/admin_users/list', component: AdminUserList },
      // 这个表示把id注入到这个组件中，在这个组件中可以使用props来接收
      { path: '/admin_users/edit/:id', component: AdminUserEdit, props: true }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 路由导航守卫
router.beforeEach((to, from, next) => {
  // 如果访问的不是登录页面，并且，没有token
  if (!to.meta.isPublic && !localStorage.getItem('token')) {
    return next('/login')
  }
  next()
})

export default router
